package com.alameda.common.config.interceptors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class MvcInterceptorConfig implements WebMvcConfigurer {
    /**
     * 允许无requestKey访问白名单
     */
    @Value("${commonpent.white.list}")
    private List<String> commonpentWhiteList;

    /**
     * 系统白名单
     */
    @Value("${system.white.list}")
    private List<String> systemWhiteList;

    @Autowired
    private ApiSecureInterceptor apiSecureInterceptor;

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private WebTraceIdInterceptor webTraceIdInterceptor;

    /**
     * 添加拦截器并指定不拦截的路径
     *
     * @param registry 拦截器注册表
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 安全拦截除组件外的所有接口
        registry.addInterceptor(apiSecureInterceptor).excludePathPatterns(commonpentWhiteList);
        // 登录拦截 登录接口和注册接口不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns(systemWhiteList);
        // 日志拦截所有请求
        registry.addInterceptor(webTraceIdInterceptor).addPathPatterns("/**");
    }
}
